public class Mining {
	private client c = null;
	public double expRate;
	public boolean isMining;
	public int OreId;
	public int emoteId;
	public int expAmount;
	public long miningDelay;
	public int[] gems = {1617, 1619, 1621, 1623 ,1631};
	public int[] pickaxeIds = {1265, 1267, 1269, 1271, 1273, 1275};
	 
	public Mining(client client) {
		this.c = client;
		this.isMining = false;
		this.OreId = -1;
		this.emoteId = 0x554;
		this.expRate = 1.0;
		this.miningDelay = -1;
	}
	 
	public client getClient() {
		return c;
	}
	
	public int getPickaxeReq() {
		switch (getPickaxetId()) {
		case 1265:
		case 1267:
			return 1;
		case 1269:
			return 5;
		case 1273:
			return 20;
		case 1271:
			return 30;
		case 1275:
			return 41;
		default:
			return 1;
		}
	}
	
	public int getPickaxetId() {
		for (int i = 0; i < pickaxeIds.length; i++) {
			if (getClient().playerEquipment[getClient().playerWeapon] == pickaxeIds[i] || getClient().playerHasItem(pickaxeIds[i])) {
				return pickaxeIds[i];
			}
		}
		return -1;
	}
	
	public void sM(final int msgIndex) {
		switch (msgIndex) {
		case 0:
			getClient().sM("You need a pickaxe to mine this ore.");
			getClient().sM("You do not have a pickaxe which you have the mining level to use.");
			break;
		case 1:
			getClient().sM("You've just advanced a Mining level! You have reached level " + getMiningLevel() + ".");
			break;
		case 2:
			getClient().sM("You swing your pickaxe at the ore.");
			break;
		case 3:
			getClient().sM("You randomy find an uncut gem.");
			break;
		case 4:
			getClient().sM("Your inventory is too full to hold any more ores.");
			break;
		default:
			getClient().sM("Mining error! Please report to Runecrafter.");
			break;
		}
	}
	
	public int getMiningLevel() {
		return getClient().playerLevel[getClient().playerMining];
	}
	
	public int getOreId() {
		switch (OreId) {
		case 2091:
			return 436;
		case 2094:
			return 438;
		case 2093:
			return 440;
		case 2097:
			return 456;
		case 2103:
			return 447;
		case 2105:
			return 449;
		default: // Normal trees + missing.
			return -1;
		}
	}
	
	public void reset() {
		this.isMining = false;
		this.OreId = -1;
		this.emoteId = 0x554;
		this.miningDelay = -1;
	}
	
	public void giveGem() {
		try {
			int gemId = gems[misc.random(gems.length)];
			ItemHandler.addItem(gemId, (getClient().absX + 1), (getClient().absY - 1), 1, getClient().playerId, false);
			sM(3);
		} catch (Exception e) {
			giveGem();
		}
	}
	
	public boolean objectIsOre(int objectId) {
		switch (objectId) {
		case 2093:
		case 2094:
		case 2097:
		case 2103:
		case 2105:
		case 2091:
			return true;
		default:
			return false;
		}
	}
	
	public double getExp() {
		switch (OreId) {
		case 2091:
			return 100;
		case 2094:
			return 120;
		case 2093:
			return 200;
		case 2097:
			return 250;
		case 2103:
			return 325;
		case 2105:
			return 400;
		default:
			return 25;
		}
	}
	
	public int getReq() {
		switch (OreId) {
		case 2091:
			return 1;
		case 2094:
			return 1;
		case 2093:
			return 15;
		case 2097:
			return 30;
		case 2103:
			return 60;
		case 2105:
			return 70;
		default: //
			return 1;
		}
	}
	
	public void startMining(int OreId) {
		this.OreId = OreId;
		if (getReq() > getMiningLevel()) {
			getClient().sM("You need a Mining level of " + getReq() + " to mine this ore.");
			return;
		}
		if (getPickaxetId() == -1 || (getPickaxeReq() > getMiningLevel())) {
			sM(0);
			return;
		}
		if (getClient().freeSlots() == 0) {
			sM(4);
			return;
		}
		if (!wieldPickaxe()) {
			int pickaxeIds = getPickaxetId();
			getClient().deleteItem(pickaxeIds, 1);
			getClient().addItem(getClient().playerEquipment[getClient().playerWeapon], 1);
			getClient().playerEquipment[getClient().playerWeapon] = pickaxeIds;
			getClient().playerEquipmentN[getClient().playerWeapon] = 1;
			getClient().setEquipment(getClient().playerEquipment[getClient().playerWeapon], getClient().playerEquipmentN[getClient().playerWeapon], getClient().playerWeapon);
		}
		getClient().pEmote = this.emoteId;
		this.isMining = true;
		this.emoteId = 0x554;
		this.miningDelay = System.currentTimeMillis();
		sM(2);
	}
	
	public long getMiningDelay() {
		switch (OreId) {
		case 2091: //Copper 
		case 2094: //Tin
			return 2500;
		case 2093://Iron
			return 4000;
		case 2097: //Coal
			return 5000;
		case 2103: // Mithril
			return 6500;
		case 2105: //Adamant
			return 7500;
		default: //
			return 2300;
		}
	}
	
	public boolean wieldPickaxe() {
		switch (getClient().playerEquipment[getClient().playerWeapon]) {
		case 1265:
		case 1267:
		case 1269:
		case 1271:
		case 1273:
		case 1275:
			return true;
		default:
			return false;
		}
	}
	public void miningProcess() {
		if (misc.random2(500) >= 499)
			giveGem();
		if (getPickaxetId() == -1 || (getPickaxeReq() > getMiningLevel()))
			return;
		if (((System.currentTimeMillis() - miningDelay) >= getMiningDelay()) && (isMining)) {
			int currentLevel = getMiningLevel();
			getClient().addItem(getOreId(), 1);
			getClient().sM("You get some " + getClient().getItemName(getOreId()).toLowerCase() + ".");
			getClient().addSkillXP((int) Math.floor(getExp() * expRate), getClient().playerMining);
			if (currentLevel < getMiningLevel()) sM(1);
			getClient().pEmote = this.emoteId;
			getClient().savegame(true);
			this.miningDelay = System.currentTimeMillis();
			if (getClient().freeSlots() == 0) {
				sM(4);
				reset();
				getClient().resetAnimation();
				return;
			}
		}
	}
}